package com.example.yangxu0703.thundermapclient.thread;

import android.annotation.SuppressLint;
import android.util.Log;

import com.example.yangxu0703.thundermapclient.iterface.IMGCallback;
import com.example.yangxu0703.thundermapclient.iterface.VEDCallback;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class SearcherThread extends Thread {

    public String path;
    public IMGCallback imgcallback;
    public VEDCallback vedioCallback;
    public int mode;

    private static String TAG = "SearcherThread";

    public SearcherThread(String path, int mode) {
        this.path = path;
        this.mode=mode;
    }

    public void setIMGCallBcak(IMGCallback imgcallback) {
        this.imgcallback = imgcallback;
    }

    public void setVedioCallback(VEDCallback vedioCallback){
        this.vedioCallback=vedioCallback;
    }

    @SuppressLint("HandlerLeak")
    @Override
    public void run() {
        super.run();

            if (path.isEmpty()) {
                if (imgcallback!=null){
                    imgcallback.sortPicBcak(null);
                }
                if (vedioCallback!=null){
                    vedioCallback.sortVedioBcak(null);
                }
            } else {
                if (mode == 1) {
                    ArrayList<ArrayList<String>> allinfo = searchAllPic(path);
                    Log.i("order", imgcallback+"==========pic" + allinfo.size());
                    if (imgcallback!=null){
                        imgcallback.sortPicBcak(allinfo);
                    }
                } else {
                    ArrayList<ArrayList<String>> allinfo = searchVideopath(path);
                    Log.i("order", vedioCallback+"==========video" + allinfo.size());
                    if (vedioCallback!=null){
                        vedioCallback.sortVedioBcak(allinfo);
                    }
                }
            }

    }

    public ArrayList<ArrayList<String>> searchAllPic(String path) {
        Log.i(TAG, "searchAllPic");
        ArrayList<ArrayList<String>> kindsRes = new ArrayList<>();
        ArrayList<String> arrayTem = null;
        String tem = "";
        int sum = 0;

        File dir = new File(path);
        if (dir.isDirectory()) {
            File[] filelist = dir.listFiles(fileFilter);
            ArrayList<File> allfile = sortinfo(filelist);
            Log.i(TAG,"allfile"+allfile.size());
            for (int i = 0; i < allfile.size(); i++) {
                //Log.i(TAG,file.getAbsolutePath());
                String info[] = allfile.get(i).getName().split("_");
                if (info.length<2){
                    continue;
                }
                if (sum == 0) {
                    arrayTem = new ArrayList<>();
                    arrayTem.add(allfile.get(i).getAbsolutePath());
                    sum = 1;
                    if (info.length<2){
                        continue;
                    }
                    tem = info[1];
                } else {
                    if (!tem.equals(info[1])) {
                        kindsRes.add(arrayTem);
                        arrayTem = new ArrayList<>();
                        arrayTem.add(allfile.get(i).getAbsolutePath());
                        tem = info[1];
                        if (i == allfile.size() - 1) {
                            kindsRes.add(arrayTem);
                        }
                    } else {
                        arrayTem.add(allfile.get(i).getAbsolutePath());
                        if (i == allfile.size() - 1) {
                            kindsRes.add(arrayTem);
                        }
                    }
                }

                if (allfile.size()==1){
                    kindsRes.add(arrayTem);
                }

            }
        }
        return kindsRes;
    }

    public ArrayList<ArrayList<String>> searchVideopath(String path) {
        Log.i(TAG, "searchVideopath");
        ArrayList<ArrayList<String>> kindsRes = new ArrayList<>();
        ArrayList<String> arrayTem = null;
        String tem = "";
        int sum = 0;

        File dir = new File(path);
        if (dir.isDirectory()) {
            File[] filelist = dir.listFiles(fileFilter);
            Log.i(TAG,filelist.length+"file count");
            ArrayList<File> allfile = sortinfo(filelist);
            for (int i = 0; i < allfile.size(); i++) {
                //Log.i(TAG,file.getAbsolutePath());
                String info[] = allfile.get(i).getName().split("_");
                if (info.length<2){
                    continue;
                }
                if (sum == 0) {
                    arrayTem = new ArrayList<>();
                    arrayTem.add(allfile.get(i).getAbsolutePath());
                    sum = 1;
                    tem = info[1];
                } else {
                    if (!tem.equals(info[1])) {
                        kindsRes.add(arrayTem);
                        arrayTem = new ArrayList<>();
                        arrayTem.add(allfile.get(i).getAbsolutePath());
                        tem = info[1];
                        if (i == allfile.size() - 1) {
                            kindsRes.add(arrayTem);
                        }
                    } else {
                        arrayTem.add(allfile.get(i).getAbsolutePath());
                        if (i == allfile.size() - 1) {
                            kindsRes.add(arrayTem);
                        }
                    }
                }
                if (allfile.size()==1){
                    kindsRes.add(arrayTem);
                }

            }
        }
        return kindsRes;
    }



    private ArrayList<File> sortinfo(File[] files) {
        ArrayList<File> sortList = new ArrayList<File>(Arrays.asList(files));
        Collections.sort(sortList, new Comparator<File>() {
            @Override
            public int compare(File file, File newFile) {
                if (file.lastModified() < newFile.lastModified()) {
                    return -1;
                } else if (file.lastModified() == newFile.lastModified()) {
                    return 0;
                } else {
                    return 1;
                }
            }

        });
        return sortList;
    }

    public FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            String tmp = file.getName().toLowerCase();
            Log.i(TAG, tmp + "----");
            if (mode==1) {
                if (tmp.endsWith(".jpg") || tmp.endsWith(".png")) {
                    return true;
                }
            }else {
                 if (tmp.endsWith(".mp4")) {
                    return true;
                }
            }
            return false;
        }
    };
}
